import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class kMeansPresenter extends JFrame{
	private kMeans kMeans;
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	JButton btnStart;
	JPanel panel;

	public kMeansPresenter(kMeans kMeans) {
	    super("KMeansClusteringPresentation");
	    setDefaultCloseOperation(HIDE_ON_CLOSE);
	    setSize(410, 480);
	    setResizable(false);
	    setLocationRelativeTo(null);
	    
	    this.kMeans = kMeans;
	    
	    panel = new JPanel();
	    panel.setPreferredSize(new Dimension(410, 480));

	    add(panel, BorderLayout.CENTER);
	 
	    setVisible(true);
	}
	
	public void present() {
		Color[] clusterColors = this.generateColorsForCluster(this.kMeans.getCluster());
		Graphics graphics = panel.getGraphics();
		
		for(DataPoint dataPoint : this.kMeans.getDataPoints()) {
	        Color oldColor = graphics.getColor();
	        if (null != dataPoint.getCluster()) {
				graphics.setColor(clusterColors[dataPoint.getCluster().getId()]);
	        }
	        graphics.fillOval((int) (dataPoint.getAttributes()[0]),
	                (int) (dataPoint.getAttributes()[1]), 5, 5);
	        graphics.setColor(oldColor);
		}

	}
	
	private Color[] generateColorsForCluster(int clusterCount) {
		Random randomizer = new Random();
		Color[] colors = new Color[clusterCount];
		for (int i = 0; i < colors.length; i++) {
			colors[i] = new Color(randomizer.nextInt(255), randomizer.nextInt(255), randomizer.nextInt(255));
		}
		return colors;
	}
		 
}
